//
//  MyInforChangeSexViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "MyInforChangeSexViewController.h"
#import "ChangeSexTableViewCell.h"
#import "ChangeSexModel.h"
@interface MyInforChangeSexViewController ()<UITableViewDelegate,UITableViewDataSource>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *datSouce;
/**  上一个数据  */
@property (strong, nonatomic) ChangeSexModel  *lastModel;
/**  选中的inexpensive  */
@property (assign, nonatomic) NSInteger  index;

@end

@implementation MyInforChangeSexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"修改性别";
    self.rightButton.hidden = NO;
    [self.rightButton setTitle:@"保存" forState:UIControlStateNormal];
    self.datSouce = [NSMutableArray array];
    NSString *nan ;
    NSString *nv;
    if ([self.sex isEqualToString:@"女"]) {
        nv = @"1";
        nan = @"0";
        self.index = 0;
    }else if([self.sex isEqualToString:@"男"]){
        nv = @"0";
        nan = @"1";
        self.index = 1;
    }else{
        nv = @"0";
        nan = @"0";
        self.index = 3;
    }
    NSArray *array = @[@{@"title": @"女", @"choose": nv},@{@"title": @"男", @"choose": nan}];
    for (NSDictionary *dci in array) {
        ChangeSexModel *model = [ChangeSexModel yy_modelWithDictionary:dci];
        if ([model.choose isEqualToString:@"1"]) {
            self.lastModel = model;
        }
        [self.datSouce addObject:model];
    }
    
    
    [self.view addSubview:self.tableView];
}

/**  保存的方法  */
- (void)action_rightButton:(UIButton *)sender{
    __weak typeof(self) weakSelf = self;
     ChangeSexModel *model = self.datSouce[self.index];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    
    paramer[@"ACCOUNT_ID"] = ZJUSERINFORMODEL.ORG_ACCOUNT.ID;
    if ([model.title isEqualToString:@"男"]) {
         paramer[@"SEX"] = @"1";
    }else{
         paramer[@"SEX"] = @"2";
    }
   
    [YJProgressHUD showMessage:@"保存中..." duration:10 view:self.view];
    [NetWorkingManager postWithUrlString:[NetWorkingURL getUpdateUser] Parameter:paramer Success:^(id object) {
        NetWorkModel *backmodel = [NetWorkModel yy_modelWithJSON:object];
        if ([backmodel.STATE isEqualToString:@"20000"]) {
            [YJProgressHUD showMessage:@"保存成功" inView:weakSelf.view];
            if (weakSelf.block) {
                weakSelf.block(model.title);
            }
            if ([model.title isEqualToString:@"男"]) {
                ZJUSERINFORMODEL.ORG_ACCOUNT.SEX = @"1";
            }else{
                ZJUSERINFORMODEL.ORG_ACCOUNT.SEX = @"2";
            }
            [ZJUserInfoModel updateUserInfoToNSUserDefaults];

            [weakSelf.navigationController popViewControllerAnimated:YES];
           
        }else{
            [YJProgressHUD showMessage:@"保存失败" inView:weakSelf.view];
        }
    } Fail:^(id object) {
        [YJProgressHUD showMessage:@"保存失败" inView:weakSelf.view];
    }];

}



/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ChangeSexTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[ChangeSexTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    cell.model = self.datSouce[indexPath.row];
    
    return cell;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChangeSexModel *model = self.datSouce[indexPath.row];
    if (self.index != indexPath.row) {
        model.choose = @"1";
        self.lastModel.choose = @"0";
        [self.tableView reloadData];
        self.lastModel = model;
        self.index = indexPath.row;

    }
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, navHeight, self.view.bounds.size.width, self.view.bounds.size.height - navHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.scrollEnabled = NO;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}




@end
